// ************************************************************
// Copyright © 2022
// File Name: light.go
// Author:
// Email:
// Created: 2022-03-14 21:48:34 (CST)
// Last Update:
//          By:
// Description:
// ************************************************************

package api

import (
	"light-pi/internal/data"
	"light-pi/internal/light"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/gotomicro/ego/core/elog"
)

func Light(c *gin.Context) {
	var action data.LightAction
	// 从网页端获取当前的动作是什么，是开灯还是关灯
	if err := c.ShouldBindJSON(&action); err != nil {
		elog.Error("get action from post method error")
		return
	}

	if action.Action == "open" {
		err := light.ControlLight(true)
		if err != nil {
			elog.Error("Switch on light error", elog.Any("err", err))
			c.JSON(http.StatusOK, "Light")
			return
		}
	} else {
		err := light.ControlLight(false)
		if err != nil {
			elog.Error("Switch off light error", elog.Any("err", err))
			c.JSON(http.StatusOK, "Light")
			return
		}
	}
}

func Index(c *gin.Context) {
	c.HTML(http.StatusOK, "index.tmpl", gin.H{
		"title": "Light House",
	})
}
